package cn.zn.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zn.base.common.constant.ScheduleConstants;
import cn.zn.quartz.dto.JobDto;
import cn.zn.quartz.service.JobService;
import cn.zn.job.entity.SysJob;
import cn.zn.job.mapper.SysJobMapper;
import cn.zn.job.service.SysJobService;
import cn.zn.job.vo.SysJobEditVo;
import cn.zn.job.vo.SysJobPageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zn-admin
 * @since 2021-01-29
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService {

    @Autowired
    private JobService jobService;

    @Override
    public Object selectJobList(SysJobPageVo vo) {
        // 列表查询
        if (!vo.getPage()) {
            return baseMapper.selectJobList(vo);
        }

        // 分页查询
        Page<SysJob> page = new Page<>(vo.getCurrent(), vo.getSize());
        return baseMapper.selectJobList(page, vo);
    }

    @Override
    public SysJob selectJobById(String jobId) {
        return baseMapper.selectById(jobId);
    }

    @Override
    @Transactional
    public void deleteJob(String[] jobIds) throws SchedulerException {
        for (String jobId : jobIds) {
            SysJob sysJob = baseMapper.selectById(jobId);
            JobDto job = new JobDto();
            BeanUtil.copyProperties(sysJob, job);
            jobService.deleteJob(job);

            baseMapper.deleteById(jobId);
        }
    }

    @Override
    @Transactional
    public void changeStatus(String jobId, SysJobEditVo vo) throws SchedulerException {
        SysJob sysJob = baseMapper.selectById(jobId);
        String status = vo.getStatus();
        sysJob.setStatus(status);
        int rows = baseMapper.updateById(sysJob);

        if (rows > 0) {
            JobDto job = new JobDto();
            BeanUtil.copyProperties(sysJob, job);
            if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
                jobService.resumeJob(job);
            } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
                jobService.pauseJob(job);
            }
        }
    }

    @Override
    public void run(String jobId) throws SchedulerException {
        SysJob sysJob = baseMapper.selectById(jobId);
        JobDto job = new JobDto();
        BeanUtil.copyProperties(sysJob, job);
        jobService.run(job);
    }

    @Override
    @Transactional
    public void insertJob(SysJobEditVo vo) throws Exception {
        SysJob sysJob = new SysJob();
        BeanUtil.copyProperties(vo, sysJob);
        sysJob.insert();

        JobDto job = new JobDto();
        BeanUtil.copyProperties(sysJob, job);
        jobService.createScheduleJob(job);
    }

    @Override
    @Transactional
    public void updateJob(String id, SysJobEditVo vo) throws SchedulerException {
        SysJob sysJob = new SysJob();
        BeanUtil.copyProperties(vo, sysJob);
        sysJob.setId(id);
        baseMapper.updateById(sysJob);

        JobDto job = new JobDto();
        BeanUtil.copyProperties(sysJob, job);
        jobService.updateJob(job);
    }


}
